import 'package:flutter/material.dart';
import 'package:chewie/chewie.dart';
import 'package:material_collection_app/utils/config.dart';
import 'package:material_collection_app/widget/common/top_bar.dart';
import 'package:video_player/video_player.dart';

class VideoPlayPage extends StatefulWidget {
  final Map arguments;
  VideoPlayPage({Key key, this.arguments}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<VideoPlayPage> {
  // 声明控制器(两个)
  VideoPlayerController videoPlayerController;
  ChewieController chewieController;
  
  @override
  void initState() {
    super.initState();
    initVideo();
  }

  @override
  void dispose() {
    videoPlayerController.dispose();
    chewieController.dispose();
    super.dispose();
  }

  initVideo() {
    var url = Config.BASE_URL + widget.arguments['video_url'];
    // 生成控制器(两个)
    videoPlayerController=VideoPlayerController.network(url);
    chewieController = ChewieController(
      videoPlayerController: videoPlayerController,
      aspectRatio: 5 / 3, //屏幕高宽比
      autoPlay: true, //是否自动播放
      looping: false, //是否循环播放
      fullScreenByDefault: false, //默认情况下全屏
      // isLive: true, //视频长度替换成live字样
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopBar(
        title: '视频播放',
      ),
      // 加载播放器
      body: Chewie(
        controller: chewieController,
      )
    );
  }
}
